layui.define(['table', 'form','laypage','laydate','layer'], function(exports){
    var $ = layui.$
        ,table = layui.table
        ,form = layui.form
        , laydate=layui.laydate
        , laypage = layui.laypage
        , layer = layui.layer;

    //路径
    var curWwwPath=window.document.location.href;
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    var localhostPaht=curWwwPath.substring(0,pos);
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);

    var limitcount = 10;
    var curnum = 1;
    table.render({
        elem: '#backupList'
        , url: localhostPaht + projectName + "/rest/system/getDBFile"
        ,title:"数据库备份数据"
        , page: false,
        height: 'full-123'
        , limit: limitcount
        , text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        }
        , cols: [[
            // {type: 'checkbox'}
            {title: 'ID', width: 70, sort: true,fixed:'left',type:'numbers'}
            , {field: 'nameurl', title: '文件名称', sort: true}
            , {field: 'username', title: '操作人员', sort: true}
            , {field: 'url', title: '文件路径', sort: true}
            , {field: 'backupdates', title: '操作时间', sort: true},
            {title: '操作', toolbar: '#recovery', fixed: "right", align: 'center', width: 200}
        ]],
        done: function (res, curr, count) {
        }
    });

    //监听工具条
    table.on('tool(backupLists)', function(obj){  //表格的lay-filter="userlists"
        var data = obj.data; //获得当前行所有数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if(obj.event === 'recovery'){
            recovery(data);
        }
    });


    function recovery(data){
        var key=data.nameurl
        layer.confirm('是否确定恢复数据？',{icon:3, title:'提示信息'}, function(index) {
            var index = layui.layer.msg('数据恢复中，请稍候', {icon: 16, time: false, shade: 0.8});
            setTimeout(function () {
                $.ajax({
                    type: "post",
                    url: localhostPaht + projectName +"/rest/system/restoreDB",
                    data:"key="+key,
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        layer.close(index);
                        if (data != "error") {
                            layer.msg('恢复成功');
                        } else {
                            layer.msg('恢复失败');
                        }
                    }
                })
            }, 2000);
        });
    }

    exports('backupList', {})
});